package cn.tedu.antconlonysever.contract.controller;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.JsonResult;
import cn.tedu.antconlonysever.contract.pojo.param.LossAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.LossUpdateParam;

import cn.tedu.antconlonysever.contract.pojo.vo.*;
import cn.tedu.antconlonysever.contract.service.ILossService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/loss")
@Api(tags = "03.流失客户管理")
@Slf4j
@Validated
public class LossController {
    @Autowired
    private ILossService lossService;

    @PostMapping("/add-new")
    @ApiOperation("添加流失客户")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid LossAddNewParam lossAddNewParam){
        log.debug("开始处理【添加流失客户】的请求，参数：{}", lossAddNewParam);
        lossService.addNew(lossAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除流失客户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除流失客户失败，请提交合法的ID值！")
                             @RequestParam Long id){
        log.debug("开始处理【根据ID删除流失客户】的请求，参数：{}", id);
       lossService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改流失客户")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "流失客户id",required = true,dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1,message = "请提交有效的订单ID值!") Long id,
                                     @Valid LossUpdateParam lossUpdateParam){
        log.debug("开始处理【修改流失客户】的请求，新数据：{}",  lossUpdateParam);
        lossService.updateInfoById(id,lossUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据id查询流失详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "订单id",required = true,dataType = "long")
    })
    public JsonResult getStandardById( @Range(min = 1,message = "根据id查询流失详情失败！请输入合法的数据参数，")
                                       @RequestParam Long id){
        log.debug("开始处理【根据id查询流失详情】的请求，iD：{}，", id);
        LossStandardVO result = lossService.getStandardById(id);
        return JsonResult.ok(result);

    }

    @GetMapping("/list")
    @ApiOperation("查询流失列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询流失列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询流失列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<LossListVO> pageData = lossService.list(page);
        return JsonResult.ok(pageData);
    }


}
